class Main extends eui.UILayer {


    protected createChildren(): void {
        super.createChildren();

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());


        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
    }

    private async loadResource() {
        try {
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            await this.loadTheme();
            await RES.loadGroup("preload", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);

        })
    }

    /**
     * 创建场景界面
     */
    protected createGameScene(): void {
        // 显示loading
        platform.showloading("登陆中。。。")
        // 环境变量
        SceneManager.ins.rootLayer = this;
        CommonData.WIN_W = this.stage.stageWidth;
        CommonData.WIN_H = this.stage.stageHeight;
        // 获取code
        platform.getCode((code) => {
            // 是否注册
            Net.ins.send(['user', 'isLogin'], { "code": code }, (e) => {
                if (e.code == 200) {
                    e = e.data;

// LoginScene
// 修改这里时别忘了改  LoginScene 里 ^^
                    // 已经注册
                    PlayerSelfData.gold = e.gold;
                    platform.token = e.openid;
                    PlayerSelfData.userinfo = e;
                    platform.userinfo = e;

                    platform.initShare();

                    // 化肥信息初始化
                    PlayerSelfData.manure["expire"] = e.fertilizer_time;
                    PlayerSelfData.manure["number"] = e.fertilizer;
                    PlayerSelfData.manure["today_use"] = e.fertilizer_use;
// 修改这里时别忘了改  LoginScene 里 $$

                    platform.hideloading()
                    //显示第一个场景
                    LoadScene.ins.load("main", new MainScene());
                } else {
                    platform.hideloading()
                    LoadScene.ins.load("login", new LoginScene());
                }

            });
        });

    }

}
